package main.com.demo.servlet;

import main.com.demo.entity.User;
import main.com.demo.service.UserService;
import main.com.demo.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 解决post乱码
        request.setCharacterEncoding("UTF-8");
        // 获取用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 调用service查询用户
        UserService service = new UserServiceImpl();
        User user = service.login(username, password);

        // 判断是否正确
        if ( user == null ) {
            response.getWriter().write("username or password is error!");
        } else {
            request.getRequestDispatcher("/register.html").forward(request, response);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
